
Static nothing but a key word. This key word uses in method, class and also constructor and variables etc. Static class contain only static property. Static constructor can’t over load by the changing parameter. Static class can’t inherit by the other class. Static class can’t create instance for access the property. It accesses by only class name use and direct access the property by the access modifier. Given bellow the example of the static key word:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectTest
{
public static class Student
{
public static int a = 5;
public static int b = 15;
public static int sum()
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("We are from Static");
Console.WriteLine("Value of A : " +Student.a);
Console.WriteLine("Value of B : " + Student.b);
Console.WriteLine("Sum of A and B : "+ Student.sum());
Console.ReadKey();
}
}
}
In this code first create static student class and then also create static field under the student class. We can see all field are static and when we call the member of the class then we can’t create instance of the class, just write the class name and access the static class member using the access modifier.